package com.yunquan.ohana.ui.bindDevice

import android.app.Application
import androidx.databinding.ObservableField
import cn.jpush.android.api.JPushInterface
import com.google.gson.Gson
import com.yunquan.ohana.R
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.binding.command.BindingConsumer
import com.yunquan.ohana.base.ui.BaseViewModel
import com.yunquan.ohana.entity.LoginEntity
import com.yunquan.ohana.http.BaseHttp
import com.yunquan.ohana.http.exception.ApiException
import com.yunquan.ohana.http.observer.BaseObserver
import com.yunquan.ohana.http.transformer.CommonTransformer
import com.yunquan.ohana.ui.login.LoginActivity
import com.yunquan.ohana.ui.main.MainActivity
import com.yunquan.ohana.utils.L
import com.yunquan.ohana.utils.SPUtils
import com.yunquan.ohana.utils.Utils
import io.reactivex.rxjava3.disposables.Disposable
import okhttp3.RequestBody.Companion.toRequestBody

class BindDeviceViewModel(application: Application) : BaseViewModel(application) {

    val code = ObservableField<String>()

    val showError = ObservableField(false)
    val errorInfo = ObservableField<String>()

    val onAccountLoginClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            startActivity(LoginActivity::class.java)
        }
    })
    val onInputChangeClick = BindingCommand(object : BindingConsumer<String> {
        override fun call(t: String) {
            if (showError.get() == true && t.isNotEmpty()) {
                showError.set(false)
            }
            if (t.length == 6) {
                L.e("验证码：${t}")
                deviceLogin(t)
            }
        }
    })

    fun deviceLogin(code: String) {
        val map = HashMap<String, Any>()
        map["qrcode"] = code
        val params = Gson().toJson(map)
        val body = params.toRequestBody(BaseHttp.MEDIA_TYPE_JSON)
        BaseHttp.api.touristLogin(body)
            .compose(CommonTransformer())
            .subscribe(object : BaseObserver<LoginEntity>() {
                override fun onSubscribe(d: Disposable) {
                    super.onSubscribe(d)
                    showLoading()
                }

                override fun onSuccess(result: LoginEntity?) {
                    super.onSuccess(result)
                    if (result != null) {
                        SPUtils.put("userId", result.deviceRoleVo!!.userId!!)
                        SPUtils.put("token", result.token!!)
                        JPushInterface.setAlias(Utils.instance.context, 1, result.deviceRoleVo.userId)
                        startActivity(MainActivity::class.java)
                        finish()
                    } else {
                        showError.set(true)
                        errorInfo.set(Utils.instance.getString(R.string.incorrect_code))
                    }
                }

                override fun onError(e: ApiException) {
                    super.onError(e)
                    showError.set(true)
                    errorInfo.set(e.message)
                }

                override fun onComplete() {
                    super.onComplete()
                    hideLoading()
                }
            })
    }
}